package com.liuzh.fitnessplan.tools

import android.app.Activity
import android.app.ActivityManager
import android.content.Context
import java.util.*
import kotlin.system.exitProcess

class ActivitysManager {

    private val activityStack : Stack<Activity> = Stack()

    companion object {
        val instance : ActivitysManager by lazy { ActivitysManager() }
    }

    fun finishActivity(className : String){
        for (activity in activityStack){
            if((activity::class.java.simpleName) == className){
                activity.finish()
                return
            }

        }
    }

    fun addActivity(activity : Activity){
        activityStack.add(activity)
    }

    fun removeActivity(activity: Activity){
        activityStack.remove(activity)
    }

    fun getTopActivity():Activity{
        return activityStack.lastElement()
    }

    fun clearActivity(){
        activityStack.forEach { it.finish() }
        activityStack.clear()
    }

    fun exitAPP(context: Context){
        clearActivity()
        val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        activityManager.killBackgroundProcesses(context.packageName)
        exitProcess(0)
    }
}